import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import {Message} from 'element-ui';
import axios from 'axios'
import Cookie from 'js-cookie'

let service = axios.create()
service.defaults.timeout = 6000 //3.5s的延迟，超过则返回错误
NProgress.configure({showSpinner: false})
axios.defaults.withCredentials = true;

service.interceptors.request.use(config => {
  config.headers.Authorization = Cookie.get('Token') // from cookie
  NProgress.start()
  return config
})
// service.interceptors.request.use(config => {
//   // console.log('config',config)  找不到option
//
//   if (getGlobal.getCookie('hasLogin')) {
//     getGlobal.setCookie('hasLogin', 1, 10)
//   }
//   NProgress.start()
//   return config
// })

service.interceptors.response.use(response => {
  // 封装与后端约定俗成的状态码代码请求中的各种
  let {config, data} = response
  if (data.code === 200) {
    NProgress.done()
    return data
  } else if (data.code === 403){
      Cookie.set('Token','')
      location.hash='#/login'
      Message({
        type: 'error',
        message: data.message
      });
    }else if (data.code===401){
      Cookie.set('Token','')
      location.href='#/login'
      Message({
        type: 'error',
        message: data.message
      });
    } else if (data.code===402){
      Cookie.set('Token','')
      location.hash='#/login'
      Message({
        type: 'error',
        message: data.message
      });
    }else if (data.code===302){
      Cookie.set('Token','')
      location.hash='#/login'
      Message({
        type: 'error',
        message: data.message
      });
    }else if (data.code===304){
      Cookie.set('Token','')
      location.href='#/login'
      Message({
        type: 'error',
        message: data.message
      });
    }else{
    Cookie.set('Token','')
    Message({
        type: 'error',
        message: data.message
      });
    }
    NProgress.done()
    return data
}, err => {
  // 此处进入错误处理，可进行错误处理封装，
  // 此处对原生http错误进行处理，
  NProgress.done()
  if (err && err.response) {
    switch (err.response.code) {
      case 400:
        err.message = '请求错误(400)';
        break;
      case 401:
        err.message = '未授权，请重新登录(401)';
        break;
      case 402:
        err.message = '未登录(402)';
        break;
      case 403:
        err.message = '抱歉您权限不足(403)';
        break;
      case 404:
        err.message = '请求出错(404)';
        break;
      case 408:
        err.message = '请求超时(408)';
        break;
      case 500:
        err.message = '服务器错误(500)';
        break;
      case 501:
        err.message = '服务未实现(501)';
        break;
      case 502:
        err.message = '网络错误(502)';
        break;
      case 503:
        err.message = '服务不可用(503)';
        break;
      case 504:
        err.message = '网络超时(504)';
        break;
      case 505:
        err.message = 'HTTP版本不受支持(505)';
        break;
      default:
        err.message = `连接出错(${err.response.code})!`;
    }
  } else {
    if (err.code == 'ECONNABORTED') {
      console.log(err.message)
      err.message = '请求超时，请稍后重试！';
    }
  }
  Message({
    type: 'error',
    message: err.message
  });
  return Promise.reject(err);
})

function get(url, params = {}, options = {}) {
  // url=url.substring(0,1)=='/' ? url: '/'+url
  return service.get(url, params, {
    headers: {
      'Content-Type': 'application/json;charset=UTF-8',
    }
  })
}

function post(url, data, options = {}) {
  // url=url.substring(0,1)=='/' ? url: '/'+url
  for (let i in data) {//检测到空值不传
    if (!data[i]) {
      delete data[i]
    }
  }
  let params = ''
  if (data) {
    params = '?'
    for (let index in data) {
      params = params + index + '=' + data[index] + '&'
    }
    params = params.substring(0, params.length - 1)
  }
  url = url + params
  return service.post(url, {}, {
    headers: {
      'Content-Type': 'application/json;charset=UTF-8',
    }
  })
}

export default {
  get,
  post
}
